package com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.FrameStructure;
import com.smedi.ismedi.authoritymanagement.core.domain.repository.FrameStructureRepository;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.model.FrameStructureEntity;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.mybatis.mapper.MenuStructureMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
@AllArgsConstructor
public class FrameStructureRepositoryImpl extends ServiceImpl<MenuStructureMapper, FrameStructureEntity> implements FrameStructureRepository, IService<FrameStructureEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public FrameStructure selectByStructureId(Long structureId) {
        FrameStructureEntity row = this.getBaseMapper().selectById(structureId);
        return dozerUtil.convertor(row, FrameStructure.class);
    }

    @Override
    public FrameStructure doInsert(FrameStructure data) {
        FrameStructureEntity frameStructureEntity = dozerUtil.convertor(data, FrameStructureEntity.class);
        this.getBaseMapper().insert(frameStructureEntity);
        return dozerUtil.convertor(frameStructureEntity, FrameStructure.class);
    }

    @Override
    public FrameStructure doUpdate(FrameStructure data) {
        FrameStructureEntity frameStructureEntity = dozerUtil.convertor(data, FrameStructureEntity.class);
        this.getBaseMapper().updateById(frameStructureEntity);
        return dozerUtil.convertor(frameStructureEntity, FrameStructure.class);
    }

    @Override
    public int selectRootCodeCount(String rootCode, Long structureId) {
        QueryWrapper<FrameStructureEntity> wrapper = new QueryWrapper();
        wrapper.eq("root_code", rootCode);
        if (structureId != null) {
            wrapper.ne("id", structureId);
        }
        return this.getBaseMapper().selectCount(wrapper);
    }

    @Override
    public List<FrameStructure> selectByParentId(Long structureId) {
        QueryWrapper<FrameStructureEntity> wrapper = new QueryWrapper();
        wrapper.eq("parent_id", structureId).orderByAsc("display_order");
        return dozerUtil.convertor(this.getBaseMapper().selectList(wrapper), FrameStructure.class);
    }

    @Override
    public List<FrameStructure> selectAllChildren() {
        QueryWrapper<FrameStructureEntity> wrapper = new QueryWrapper();
        wrapper.orderByAsc("display_order");
        return dozerUtil.convertor(this.getBaseMapper().selectList(wrapper), FrameStructure.class);
    }

    @Override
    public void doDelete(Long structureId) {
        this.getBaseMapper().deleteById(structureId);
    }

    @Override
    public FrameStructure selectByRootCode(String rootCode) {
        QueryWrapper<FrameStructureEntity> wrapper = new QueryWrapper();
        wrapper.eq("root_code", rootCode);
        return dozerUtil.convertor(this.getBaseMapper().selectOne(wrapper), FrameStructure.class);
    }
}
